<template>
  <el-aside width="100%" class="quick-links">
    <el-card class="card" header="系统链接">
      <div class="grid-container">
        <div v-for="item in systemLinks" :key="item.id" class="grid-item" @click="handleClick(item)">
          <el-icon class="grid-icon"><component :is="item.icon" /></el-icon>
          <div class="grid-content" :title="item.name">{{ item.name }}</div>
        </div>
      </div>
    </el-card>
    <el-card class="card" header="动态消息" style="margin-top: 16px">
      <div v-for="item in newsList" :key="item.id" class="news-item">
        <span class="system-name">{{ item.systemName }}</span>
        <span class="todo-content">{{ item.content }}</span>
        <el-button type="primary" size="small" @click="handleViewTodo(item)">查看</el-button>
      </div>
    </el-card>
    <el-card class="card" header="系统预警" style="margin-top: 16px">
      <div v-for="item in alertList" :key="item.id" class="alert-item" :class="item.level">
        <span class="system-name">{{ item.systemName }}</span>
        <span class="alert-content">{{ item.content }}</span>
        <el-button type="primary" size="small" @click="handleViewAlert(item)">查看</el-button>
      </div>
    </el-card>
  </el-aside>
</template>

<script lang="ts" setup>
  import router from '@/router'
  import { Monitor, DataLine, Connection, Histogram, View } from '@element-plus/icons-vue'
  import { ElMessage } from 'element-plus'

  interface SystemLink {
    id: string
    name: string
    url: string
    icon: string
  }

  interface TodoItem {
    id: string
    systemName: string
    content: string
  }

  interface AlertItem {
    id: string
    systemName: string
    content: string
    level: 'high' | 'medium' | 'low'
    timestamp: string
  }

  const systemLinks: SystemLink[] = [
    {
      id: 'dcm',
      name: 'DCM 钻完井管理系统',
      url: 'https://dcm.cnooc/enter',
      icon: 'Monitor',
    },
    {
      id: 'iadm',
      name: 'IADM 钻完井作业智能分析及动态管理系统',
      url: '/iadm',
      icon: 'DataLine',
    },
    {
      id: 'topdrive',
      name: '顶驱系统',
      url: '/topdrive',
      icon: 'Connection',
    },
    {
      id: 'logging',
      name: '测井系统',
      url: '/logging',
      icon: 'Histogram',
    },
    {
      id: 'skyeye',
      name: '天眼系统',
      url: '/skyeye',
      icon: 'View',
    },
  ]

  const newsList: TodoItem[] = [
    {
      id: '1',
      systemName: 'DCM系统',
      content: '待办事项1',
    },
    {
      id: '2',
      systemName: 'IADM系统',
      content: '待办事项2',
    },
    {
      id: '3',
      systemName: '顶驱系统',
      content: '待办事项3',
    },
  ]

  const alertList: AlertItem[] = [
    {
      id: '1',
      systemName: 'DCM系统',
      content: '钻井参数异常，请及时检查',
      level: 'high',
      timestamp: '2024-03-20 10:30:00',
    },
    {
      id: '2',
      systemName: 'IADM系统',
      content: '设备运行状态需要关注',
      level: 'medium',
      timestamp: '2024-03-20 09:15:00',
    },
    {
      id: '3',
      systemName: '顶驱系统',
      content: '系统运行正常，建议定期维护',
      level: 'low',
      timestamp: '2024-03-20 08:45:00',
    },
  ]

  const handleClick = async (system: System) => {
    const token = localStorage.getItem('token')
    if (token) {
      // 模拟单点登录
      ElMessage.success(`正在访问${system.name}...`)
      // 实际项目中，这里应该调用后端接口进行单点登录
      // window.open(system.url, '_blank')
      try {
        await window.electronAPI.openUrl(system.url)
      } catch (error) {
        console.error(error)
      }
    } else {
      ElMessage.error('请先登录')
      router.push('/login')
    }
  }

  const handleViewTodo = (todo: TodoItem) => {
    ElMessage.info(`查看${todo.systemName}的${todo.content}`)
  }

  const handleViewAlert = (alert: AlertItem) => {
    ElMessage.info(`查看${alert.systemName}的预警信息：${alert.content}`)
  }
</script>

<style lang="scss" scoped>
  .quick-links {
    background: transparent;
    padding: 8px 0;

    .card {
      margin-bottom: 8px;

      .grid-container {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 12px;
        padding: 8px 0;

        .grid-item {
          background-color: #f5f7fa;
          border-radius: 4px;
          padding: 16px 12px;
          text-align: center;
          cursor: pointer;
          transition: all 0.3s;
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 8px;

          &:hover {
            background-color: #e6f1fc;
            transform: translateY(-2px);
          }

          .grid-icon {
            font-size: 24px;
            color: #409eff;
          }

          .grid-content {
            font-size: 14px;
            color: #606266;
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }

      .card-item {
        margin-bottom: 6px;
        font-size: 14px;
      }

      .news-item {
        display: flex;
        align-items: center;
        padding: 8px 0;
        border-bottom: 1px solid #ebeef5;
        &:last-child {
          border-bottom: none;
        }

        .system-name {
          color: #409eff;
          font-weight: 500;
          margin-right: 12px;
          min-width: 100px;
          font-size: 14px;
        }

        .todo-content {
          flex: 1;
          color: #606266;
          font-size: 14px;
        }
      }

      .alert-item {
        display: flex;
        align-items: center;
        padding: 12px;
        margin-bottom: 8px;
        border-radius: 4px;
        border-left: 4px solid;

        &.high {
          background-color: #fef0f0;
          border-left-color: #f56c6c;
        }

        &.medium {
          background-color: #fdf6ec;
          border-left-color: #e6a23c;
        }

        &.low {
          background-color: #f0f9eb;
          border-left-color: #67c23a;
        }

        .system-name {
          color: #303133;
          font-weight: 500;
          margin-right: 12px;
          min-width: 100px;
          font-size: 14px;
        }

        .alert-content {
          flex: 1;
          color: #606266;
          font-size: 14px;
        }
      }
    }
  }
</style>
